package com.csii.mobilebanking.user;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.acl.Group;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.xmlpull.v1.XmlPullParserException;
import com.csii.mobilebanking.R;
import com.csii.mobilebanking.funbox.MainDescript;
import com.csii.mobilebanking.funbox.PluginItem;
import com.csii.mobilebanking.funbox.jar.PluginBuilder;
import com.csii.mobilebanking.funbox.jar.PluginDescription;
import com.csii.mobilebanking.funbox.jar.PluginInvoke;
import com.csii.mobilebanking.funbox.jar.PluginSearch;
import com.csii.mobilebanking.funbox.jar.beans.Plugin;
import com.csii.mobilebanking.funbox.jar.beans.PluginFeature;
import com.csii.mobilebanking.funbox.jar.beans.PluginFeatureMethod;
import com.csii.mobilebanking.funbox.jar.beans.PluginIntent;

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

/**
 * 帮助
 * @author Administrator
 *
 */
public class HelpExpListAc extends Activity implements OnClickListener {

	ExpandableListView mExpList;
	ExpListAdt mExpListAd;
	WebView mWebView;
	String mKey = "comm1";
	TextView tv_home;
	private LinearLayout llPluginList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.expandlist);
		initView();
		regListener();
	}

	private void regListener() {
		tv_home.setOnClickListener(this);

	}

	private void initView() {
		mWebView = (WebView) findViewById(R.id.help_inf_content);
		tv_home = (TextView) findViewById(R.id.help_tv_home);
		mExpList = (ExpandableListView) findViewById(R.id.elist);
		llPluginList = (LinearLayout) findViewById(R.id.llPluginList);
		// mExpListAd = new ExpListAd(this);
		// mExpList.setAdapter(mExpListAd);
		List<GroupBean> datas = oldDatas();
		List<GroupBean> addDatas = getDatas(datas);
		mExpListAd = new ExpListAdt(this, addDatas);
		mExpList.setAdapter(mExpListAd);
		// mExpList.expandGroup(0);
		mExpList.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return false;
			}
		});

		mExpList.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// String mKey =
				// HpLotto.getHelpFileName(groupPosition,childPosition); 二维数组

				String mFileName = mKey + ".html";
				String mFilePath = (new StringBuilder())
						.append("file:///android_asset/help/")
						.append(mFileName).toString();
				mWebView.loadUrl(mFilePath);
				return false;
			}
		});
		// mExpList.setAdapter(new GroupChildUtil(this,addDatas).bindAdapter());
	}

	private List<GroupBean> oldDatas() {
		List<GroupBean> gList = new ArrayList<GroupBean>();
		List<ChildBean> cList1 = new LinkedList<ChildBean>();
		List<ChildBean> cList2 = new LinkedList<ChildBean>();
		GroupBean gBean = new GroupBean();
		gBean.setId("old_id_g1");
		gBean.setName("移动银行简介");

		ChildBean cBean1 = new ChildBean();
		cBean1.setId("old_id_c1");
		cBean1.setName("客户信息1");
		ChildBean cBean2 = new ChildBean();
		cBean2.setId("old_id_c2");
		cBean2.setName("客户信息2");
		ChildBean cBean3 = new ChildBean();
		cBean3.setId("old_id_c3");
		cBean3.setName("客户信息3");
		cList1.add(cBean1);
		cList1.add(cBean2);
		cList1.add(cBean3);
		gBean.setListChildBeans(cList1);

		GroupBean gBean2 = new GroupBean();
		gBean2.setId("old_id_g2");
		gBean2.setName("常见问题");

		ChildBean cBean21 = new ChildBean();
		cBean21.setId("old_id_c1");
		cBean21.setName("辅助工具1");
		ChildBean cBean22 = new ChildBean();
		cBean22.setId("old_id_c2");
		cBean22.setName("辅助工具2");
		cList2.add(cBean21);
		cList2.add(cBean22);

		gBean2.setListChildBeans(cList2);

		gList.add(gBean);
		gList.add(gBean2);
		return gList;
	}

	private List<GroupBean> getDatas(List<GroupBean> datas) {
		List<GroupBean> gList = new LinkedList<GroupBean>();
		List<ChildBean> cList1 = new LinkedList<ChildBean>();
		List<ChildBean> cList2 = new LinkedList<ChildBean>();
		GroupBean gBean = new GroupBean();
		gBean.setId("new_id_g1");
		gBean.setName("开通使用指南");

		ChildBean cBean1 = new ChildBean();
		cBean1.setId("new_id_c1");
		cBean1.setName("网店助手1");
		ChildBean cBean2 = new ChildBean();
		cBean2.setId("new_id_c2");
		cBean2.setName("网店助手2");
		ChildBean cBean3 = new ChildBean();
		cBean3.setId("new_id_c3");
		cBean3.setName("网店助手3");
		cList1.add(cBean1);
		cList1.add(cBean2);
		cList1.add(cBean3);
		gBean.setListChildBeans(cList1);

		GroupBean gBean2 = new GroupBean();
		gBean2.setId("new_id_g2");
		gBean2.setName("安全服务");

		ChildBean cBean21 = new ChildBean();
		cBean21.setId("new_id_c1");
		cBean21.setName("银行经理助手1");
		ChildBean cBean22 = new ChildBean();
		cBean22.setId("new_id_c2");
		cBean22.setName("银行经理助手2");

		cList2.add(cBean21);
		cList2.add(cBean22);

		gBean2.setListChildBeans(cList2);

		for (GroupBean bean : datas) {
			gList.add(bean);
		}
		gList.add(gBean);
		gList.add(gBean2);

		return gList;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.help_tv_home:
			GlobalJumpAc.goFirstAc(this);

			break;

		default:
			break;
		}

	}

	/**
	 * 这里程序查找扩展
	 * 
	 * @throws NameNotFoundException
	 * @throws XmlPullParserException
	 * @throws IOException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 */
	private void findMyPlugin() throws NameNotFoundException, IOException,
			XmlPullParserException, ClassNotFoundException,
			IllegalAccessException, InstantiationException,
			IllegalArgumentException, SecurityException, NoSuchFieldException {

		// 首先，就是查找插件
		PluginSearch psearch = new PluginSearch();
		List<Plugin> plugins = psearch.getPlugins(this);

		// 然后将插件再组装一下
		PluginBuilder pbuilder = new PluginBuilder(this);
		plugins = pbuilder.buildPluginsDescrition(plugins);

		// 显示出所有插件
		flateUI(plugins);
	}

	/**
	 * 将所有插件列出来
	 * 
	 * @param plugins
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws NameNotFoundException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 */
	private void flateUI(List<Plugin> plugins) throws ClassNotFoundException,
			IllegalAccessException, InstantiationException,
			NameNotFoundException, IllegalArgumentException, SecurityException,
			NoSuchFieldException {

		for (final Plugin plug : plugins) {
			// 加入插件
			PluginItem item = new PluginItem(this);

			// 获取插件的描述对象
			PluginDescription<MainDescript> pdes = new PluginDescription<MainDescript>(
					MainDescript.class);

			MainDescript des = pdes.getDescription(this, plug);

			// 设置插件item的描述信息
			item.setDes(des.getDescription());
			// 插件的状态
			int status = plug.getStatus();

			Drawable d = plug.getContext().getResources()
					.getDrawable(des.getIconResId());
			item.setLogoImage(d);
			// 获得当前插件的功能
			List<PluginFeature> features = plug.getFeatures();

			// 遍历功能
			// for (final PluginFeature pf : features) {
			//
			// for (final PluginFeatureMethod fm : pf.getMethods()) {

			// item.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// PluginInvoke pi = new PluginInvoke(ExpListAc.this);
			//
			// try {
			// pi.invoke(plug, pf, fm);
			// } catch (Exception e) {
			// e.printStackTrace();
			// }
			// }
			// });
			// 添加插件，并且设置执行事件
			// item.addPluginMethod(fm, new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// PluginInvoke pi = new PluginInvoke(
			// ExpListAc.this);
			//
			// try {
			// pi.invoke(plug, pf, fm);
			// } catch (SecurityException e) {
			// e.printStackTrace();
			// } catch (IllegalArgumentException e) {
			// e.printStackTrace();
			// } catch (NameNotFoundException e) {
			// e.printStackTrace();
			// } catch (ClassNotFoundException e) {
			// e.printStackTrace();
			// } catch (IllegalAccessException e) {
			// e.printStackTrace();
			// } catch (InstantiationException e) {
			// e.printStackTrace();
			// } catch (NoSuchMethodException e) {
			// e.printStackTrace();
			// } catch (InvocationTargetException e) {
			// e.printStackTrace();
			// }
			// }
			// });
			// }
			// }

			// 遍历intent -只有一个啊？
			Map<String, PluginIntent> intents = plug.getIntents();

			Iterator<Map.Entry<String, PluginIntent>> iterator = intents
					.entrySet().iterator();

			while (iterator.hasNext()) {
				final Map.Entry<String, PluginIntent> entry = iterator.next();
				item.addPluginMethod(entry.getValue(), new OnClickListener() {
					@Override
					public void onClick(View v) {
						PluginInvoke pi = new PluginInvoke(HelpExpListAc.this);
						pi.invoke(entry.getValue());
					}
				});

			}
			// 将插件加入到ui
			llPluginList.addView(item);
		}
	}
}
